package com.sina.shop.domain.admin;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.sina.shop.core.domain.Domain;
import com.sina.shop.domain.Status;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 权限表
 * 段浩杰   2017-12-08-17:53
 */
@Entity
@Table(name = "tb_permission")
@DynamicInsert
@DynamicUpdate
public class Permission extends Domain {

    @Id
    @Column(name = "id", updatable = false)
    @GenericGenerator(name = "sequence_id", strategy = "com.sina.shop.sequence.IdGenerator")
    @GeneratedValue(generator = "sequence_id")
    @Min(1)
    private Long id;//主键

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinColumn(name = "pid")
    @JsonIgnore
    private Permission permission; //父id

    @JsonIgnore
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "permission", fetch = FetchType.LAZY)
    private Set<Permission> permissions;//所有孩子

    @Column(name = "name", length = 20)
    private String name;//权限名字

    @Enumerated(EnumType.STRING)
    @Column(name = "type", length = 10)
    private Type type;//权限类型

    @Column(name = "sort", length = 5)
    private Integer sort;//排序

    @Column(name = "perm_code", length = 20)
    private String permCode;//权限编码

    @Column(name = "description", length = 100)
    private String description;//描述

    @Column(name = "create_time", updatable = false)
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;//创建时间

    @Column(name = "update_time")
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime updateTime;//更新时间

    @Column(name = "create_name", length = 20)
    private String createName;//创建者

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 10)
    private Status status;//状态

    @JsonIgnore
    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "permissions", fetch = FetchType.LAZY)
    private Set<Role> roles;//角色关联

    public Permission() {
    }

    public Permission(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getPermCode() {
        return permCode;
    }

    public void setPermCode(String permCode) {
        this.permCode = permCode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}
